// pages/beebox/addbeebox.js
import http from '../../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fxcode:'',
    array:[],
    pars:{
      apiaryId:'',
      apiaryName:'',
      name:'',
      deviceCode:''
    },
    activeindex:-1,
    ac_name:false,
    ac_code:false,
    inputshow:false,
    toast:false
  },
  onLoad:function(options){
    console.log(options)
    if (options.type=='add'){
      this.setData({
        inputshow:true,
      })
    }
    if (options.type == 'scan') {
      this.setData({
        inputshow_1: true,
      })
    }
    if (options.showtoast){
      this.setData({
        toast: options.showtoast
      })
    }
    let _this=this
    this.getapiary()
    wx.getStorage({
      key: 'code',
      success(res) {
        _this.setData({
          fxcode:res.data,
          ['pars.deviceCode']:res.data
        })
      }
    })
  },
  onShow:function(){
    if (this.data.toast && this.data.inputshow_1){
      wx.showToast({
        title: '无法识别的二维码',
        icon: 'none',
        mask: true,
        duration: 2000
      })
    }
  },
  // 初始化蜂场下拉
  getapiary:function(){
    http.getdata('/api/apiary/list').then((res)=>{
      this.setData({
        array: res.data.result.records
      })
    })
  },
  // 归属峰场
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      activeindex: e.detail.value,
      ['pars.apiaryName']: this.data.array[e.detail.value].name,
      ['pars.apiaryId']: this.data.array[e.detail.value].id,
    })
  },
  // 提示框
  viewbee: function () {
    if (this.data.activeindex==-1){
      wx.showToast({
        title: '请选择归属蜂场',
        icon:'none',
        mask:true
      })
    }else{
      http.postdata('/api/beehive/add',this.data.pars).then((res)=>{
       wx.redirectTo({
         url: '../../home/home',
       })
      })
    }
  },
  // 获取蜂箱名称
  getname: function (e) {
    let type = e.currentTarget.dataset.type
    if (type =="ac_name"){
      this.setData({
        ['pars.name']: e.detail.value
      })
    }else{
      this.setData({
        ['pars.deviceCode']: e.detail.value
      })
    }
  
  },
  active: function (e) {
    let type = e.currentTarget.dataset.type
    if (type =="ac_code"){
      this.setData({
        ac_code: true,
        ac_name: false
      })
    }else{
      this.setData({
        ac_code: false,
        ac_name: true
      })
    }
  },
  // unactive:function(){
  //   this.setData({
  //     ac_name: false
  //   })
  // }
})
